package cn.sirun.com.fca.account.view.impl;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.tencent.cloud.iov.flow.view.IContentViewContainer;
import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.kernel.util.CookieUtils;
import com.tencent.cloud.iov.router.Router;
import com.tencent.cloud.iov.util.ActivityUtils;
import com.tencent.cloud.uikit.ui.widget.IovToolbar;
import com.tencent.cloud.uikit.utils.SystemUiUtils;
import com.tencent.cloud.uikit.widget.edittext.PasswordEditText;

import cn.sirun.com.fca.account.R;
import cn.sirun.com.fca.account.constant.AccountActivityConst;
import cn.sirun.com.fca.account.constant.AccountCommonConst;
import cn.sirun.com.fca.account.constant.AccountIntentConst;
import cn.sirun.com.fca.account.model.login.AuthenticateResponse;
import cn.sirun.com.fca.account.model.login.ChallengeResponse;
import cn.sirun.com.fca.account.model.login.LoginResponse;
import cn.sirun.com.fca.account.model.login.VehicleRespons;
import cn.sirun.com.fca.account.presenter.ILoginPresenter;
import cn.sirun.com.fca.account.presenter.impl.LoginPresenter;
import cn.sirun.com.fca.account.service.LoginManager;
import cn.sirun.com.fca.account.view.ILoginView;

public class ThirdTwoLoginFlowView extends AccountBaseLoadingFlowView<ILoginPresenter>
        implements View.OnClickListener{

    private PasswordEditText mPassET;
    private TextView mForgetPassTV,mLoginTV;

    public ThirdTwoLoginFlowView(Activity activity) {
        this(activity, null);
    }

    public ThirdTwoLoginFlowView(Activity activity, IContentViewContainer container) {
        super(activity, container);
        CookieUtils.clear();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        setUpActionBar();
        initView();
    }


    @Override
    protected ILoginPresenter createPresenter() {
        return new LoginPresenter();
    }

    private void initView() {
        setContentView(R.layout.thirdlogin_two_activity);
        final Activity activity = getActivity();

        mPassET = activity.findViewById(R.id.et_thirdpass_number);
        mForgetPassTV = activity.findViewById(R.id.tv_forget_thirdpass);
        mForgetPassTV.setOnClickListener(this);
        mLoginTV = activity.findViewById(R.id.tv_third_login);
        mLoginTV.setOnClickListener(this);
    }

    private void setUpActionBar() {
        final IovToolbar toolbar = SystemUiUtils.showActionBar(
                getActivity(), "");
        toolbar.setHomeAsUpText(R.string.tv_title_back);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (AccountCommonConst.FINISH_PREVIOUS_ACTIVITY_RESULT_CODE == resultCode) {
            getActivity().finish();
        } else if (AccountCommonConst.TURN_TO_MAIN_ACTIVITY_RESULT_CODE == resultCode) {
//            if (AccountIntentConst.MODIFY_LOGIN_PASSWORD == mFrom) {
//                getActivity().finish();
//            } else {
            turnToMainActivity();
//            }
        } else if (AccountCommonConst.QQ_AUTH_REQUEST_CODE == requestCode) {
//            handleActivityResult(requestCode, resultCode, data);
        }
    }

    public void turnToMainActivity() {
        Intent intent = Router.fromPath(AccountActivityConst.Path.MAIN);
        ActivityUtils.startActivity(getActivity(), intent);
        getActivity().finish();
    }

    private void handleLoginSuccess(LoginResponse response) {
        LoginManager.getInstance().handleLoginSuccess(response);
//        getPresenter().getVehicle();
        turnToMainActivity();
//        if (AccountIntentConst.OPEN_BIND_LOGIN == mFrom) {
//            //互联登录绑定跳转过来的登录，发送onBind请求
//            getPresenter().openBind(mBindRequest);
//        } else {
//            // 若是登录态失效，则销毁当前界面，转回失效前界面
//            if (AccountCommonConst.FINISH_PREVIOUS_ACTIVITY_RESULT_CODE == mFrom
//                    || AccountIntentConst.MODIFY_LOGIN_PASSWORD == mFrom) {
//                getActivity().finish();
//            } else {
//                // 若为正常登录（非登录态失效），则跳转到首页
//                turnToMainActivity();
//            }
//        }
    }

    private void forgetPassIntent() {
        String path = AccountActivityConst.Path.PHONE;
        Intent intent = Router.fromPath(path);
        intent.putExtra(AccountIntentConst.FROM, AccountIntentConst.FORGET_PASSWORD);
        getActivity().startActivity(intent);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tv_forget_thirdpass) {
            forgetPassIntent();
        }else if(id==R.id.tv_third_login){

        }
    }

}
